$.ajaxSetup({
	error : function(XMLHttpRequest, textStatus, errorThrown) {
		$.messager.progress('close');
		$.messager.alert('错误', XMLHttpRequest.responseText);
	}
});

function pagerFilter(data) {
	if (typeof data.length == 'number' && typeof data.splice == 'function') {
		data = {
			total : data.length,
			rows : data
		};
	}
	var dg = $(this);
	var opts = dg.datagrid('options');
	var pager = dg.datagrid('getPager');
	pager.pagination({
		onSelectPage : function(pageNum, pageSize) {
			opts.pageNumber = pageNum;
			opts.pageSize = pageSize;
			pager.pagination('refresh', {
				pageNumber : pageNum,
				pageSize : pageSize
			});
			dg.datagrid('loadData', data);
		}
	});
	if (!data.originalRows) {
		data.originalRows = (data.rows);
	}
	var start = (opts.pageNumber - 1) * parseInt(opts.pageSize);
	var end = start + parseInt(opts.pageSize);
	data.rows = (data.originalRows.slice(start, end));
	return data;
}

function tableBtn(value, row, index, model) {
	return '<span class="icon icon-edit" onclick="onEdit(' + value
	+ ',\''+ model + '\')" title="修改"></span><span class="icon icon-delete" onclick="onDelete(' + value
			+ ',\''+ model + '\')" title="删除"></span>';
}
function onAdd(model) {
	$('#dlg').dialog({
		title : '新建',
		href : model + '-form.html',
		width : 800,
		height : 400,
		buttons : [ {
			text : '确定',
			handler : function() {
				var options = {
					url : 'app/' + model,
					dataType : 'json',
					type : 'POST',
					success : function(data) {
						$('#dlg').dialog('close');
						$('#' + model + '-table').datagrid('reload');
					}
				};
				$("#" + model + "-form").ajaxSubmit(options);
			}
		}, {
			text : '取消',
			handler : function() {
				$('#dlg').dialog('close');
			}
		} ]
	});
	$('#dlg').dialog('open');
}
function onQuery(model) {
	$('#' + model + '-table').datagrid('reload');
}
function onEdit(id, model) {
	$('#dlg').dialog({
		onLoad : function() {
			$('#' + model + '-form').form('load', 'app/'+ model + '/' + id);
		},
		title : '修改',
		href : model + '-form.html',
		width : 800,
		height : 400,
		buttons : [ {
			text : '确定',
			handler : function() {
				var options = {
					url : 'app/' + model + '/' + id,
					dataType : 'json',
					type : 'POST',
					success : function(data) {
						$('#dlg').dialog('close');
						$('#' + model + '-table').datagrid('reload');
					}
				};
				$("#" + model + "-form").ajaxSubmit(options);
			}
		}, {
			text : '取消',
			handler : function() {
				$('#dlg').dialog('close');
			}
		} ]
	});
}
function onDelete(id, model) {
	$.ajax({
		url : 'app/' + model + "/" + id,
		type : "delete",
		success : function(date) {
			$('#' + model + '-table').datagrid('reload');
		}
	});
}
function artBtn(value,row, index){
	return tableBtn(value,row,index,'art');
}
function userBtn(value,row, index){
	return tableBtn(value,row,index,'user');
}
function siteBtn(value,row, index){
	return tableBtn(value,row,index,'site');
}
function requestBtn(value,row, index){
	return tableBtn(value,row,index,'request');
}
function taskBtn(value,row, index){
	return tableBtn(value,row,index,'task');
}

function formartString(str) {
	for ( var i = 0; i < arguments.length - 1; i++) {
		str = str.replace("{" + i + "}", arguments[i + 1]);
	}
	return str;
}

function formatCol(value, row, index) {
	return row.format;
}
function drawInput(value, row, index) {
	return "<input type='text' />";
}
function drawInputWithValue(value, row, index) {
	if(value == null){
		return "<input type='text' value=''/>";
	}
	return "<input type='text' value='" + value + "'/>";
}

function getRowIndex(target){
	var tr = $(target).closest('tr.datagrid-row');
	return parseInt(tr.attr('datagrid-row-index'));
}


function tip(msg, fn) {
	$.messager.alert('提示', msg, 'info', fn);
}
function success(msg, fn) {
	$.messager.alert('提示', msg, 'success', fn);
}
function error(msg, fn) {
	$.messager.alert('错误', '地区新增失败!', 'error', fn);
}

function confirm(msg, fn) {
	$.messager.confirm('确认', msg, fn);
}

